package com.hospital.workstation.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.lang.Console;
import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.hospital.common.annotation.RepeatSubmit;
import com.hospital.common.annotation.Log;
import com.hospital.common.core.controller.BaseController;
import com.hospital.common.core.domain.PageQuery;
import com.hospital.common.core.domain.R;
import com.hospital.common.core.validate.AddGroup;
import com.hospital.common.core.validate.EditGroup;
import com.hospital.common.core.validate.QueryGroup;
import com.hospital.common.enums.BusinessType;
import com.hospital.common.utils.poi.ExcelUtil;
import com.hospital.workstation.domain.vo.MedicationRecordVo;
import com.hospital.workstation.domain.bo.MedicationRecordBo;
import com.hospital.workstation.service.IMedicationRecordService;
import com.hospital.common.core.page.TableDataInfo;

/**
 * 药历首页信息
 *
 * @author whb
 * @date 2023-02-13
 */
@Validated
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/workstation/medicationRecord")
public class MedicationRecordController extends BaseController {

    private final IMedicationRecordService iMedicationRecordService;

    /**
     * 获取药历首页信息详细信息
     *
     * @param medicalRecordNo 病历号
     */
    @SaCheckPermission("workstation:medicationRecord:query")
    @GetMapping("/{medicalRecordNo}")
    public R<MedicationRecordVo> getInfo(@NotNull(message = "病历号不能为空")
                                         @PathVariable Integer medicalRecordNo) {
        return R.ok(iMedicationRecordService.queryByMedicalRecordNo(medicalRecordNo));
    }

    /**
     * 保存药历首页信息
     */
    @SaCheckPermission("workstation:medicationRecord:add")
    @Log(title = "药历首页信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MedicationRecordBo bo) {
        return iMedicationRecordService.insertByBo(bo);
    }

    /**
     * 下载药历
     *
     * @param request
     * @param response
     * @param medicalRecordNo 病历号
     */
    @SaCheckPermission("workstation:medicationRecord:download")
    @GetMapping("/download/{medicalRecordNo}")
    public void download(HttpServletRequest request, HttpServletResponse response, @PathVariable Integer medicalRecordNo) {
        iMedicationRecordService.exportWord(response, medicalRecordNo);
    }

    /**
     * 下载全面药历
     *
     * @param response
     * @param medicalRecordNo 病历号
     */
    @SaCheckPermission("workstation:medicationRecord:overall")
    @GetMapping("/overall/{medicalRecordNo}")
    public void overall(HttpServletResponse response, @PathVariable Integer medicalRecordNo) {
        iMedicationRecordService.overallWord(response, medicalRecordNo);
    }
}
